<?php

/**
 * return an array of the post types that we have set up post connectors for
 * @param return array
 */

function wp_fields_admin_menu() {
	add_submenu_page( 'options-general.php' , WP_FIELDS_NAME, WP_FIELDS_NAME, "administrator", "wp-fields-options", "wp_fields_options");
}

	
function wp_fields_options() {
	
	$field_groups = get_option("wp_fields_groups");
	if (!$field_groups) {
		$field_groups = array();
	}
	/*Sort*/
	function wp_fields_uasort($a, $b) {
		if ($a["name"] == $b["name"]) { return 0; }
		return strcasecmp($a["name"], $b["name"]);
	}
	uasort($field_groups, "wp_fields_uasort");
?>
	<div class="wpfields">
		<h2><?php echo WP_FIELDS_NAME ?></h2>
        <?php 
		
		$action = (isset($_GET["action"])) ? $_GET["action"] : null;
		
		//New group creation
		if ("edit-field-group" == $action) {
		
		$field_group_id = (int) $_GET["group-id"];
		$highest_field_id = 0;
		
		// if new, save it as unnamed, and then set to edit that
			if ($field_group_id == 0) {
				$highest_id = 0;
				foreach ($field_groups as $oneGroup) {
					if ($oneGroup["id"]>$highest_id) {
						$highest_id = $oneGroup["id"];
					}
				}
				$highest_id++;
				$field_group_id = $highest_id;
				
				$field_groups[$field_group_id] = array(
					"id" => $field_group_id,
					"name" => "Unnamed field group $field_group_id",
					"description" => "Description for group $field_group_id",
					"repeatable" => false,
					"fields" => array(),
					"deleted" => false
				);
				
				update_option("wp_fields_groups", $field_groups);

			} else {
				// existing field group
				// get highest group and field id
				//var_dump($field_groups[$field_group_id]["fields"]);
				foreach ($field_groups[$field_group_id]["fields"] as $one_field) {
					if ($one_field["id"] > $highest_field_id) {
						$highest_field_id = $one_field["id"];
					}
				}
			}		
			$field_group_in_edit = $field_groups[$field_group_id];		
		?>
		<form method="post" action="<?php echo WP_FIELDS_FILE ?>&amp;action=edit-field-group-save">	
            <div class="hr hr-listing">&nbsp;</div>
     <div class="outer-field-group">
     	<div class="field-group">
    	<div class="title field-group-header">
        	<h3>Drag n Drop fields</h3>
            <div class="field-operations"><a href="#" id="clear-group">Clear group</a></div>
            <label><input type="checkbox" value="" />Repeatable</label>
        </div>
        <div id="field-group-drop">
        	<?php
				foreach ($field_group_in_edit["fields"] as $oneField) {
					//if (!$oneField["deleted"]) {
						echo wp_fields_field_group_add_field_template($oneField["id"], $field_group_in_edit);
						
					//}
				}
			?>
        </div>
    </div>
    <div class="form-buttons">
    		<div class="group-details">
            	<label>Group name: <input type="text" name="field_group_name" id="wp_field_group_name" class="txt" value="<?php echo esc_html($field_group_in_edit["name"]) ?>" /></label>
                <label>Group desc: <input type="text" name="field_group_description" id="wp_field_group_description" class="regular-text" value="<?php echo esc_html($field_group_in_edit["description"]) ?>" /></label>
                <input type="hidden" name="action" value="update" />
			    <input type="hidden" name="page_options" value="field_group_name" />
				<input type="hidden" name="field_group_id" value="<?php echo $field_group_in_edit["id"] ?>" />
            </div>
            <a class="btn-cancel" href="<?php echo WP_FIELDS_FILE ?>"><img src="<?php echo WP_FIELDS_URL ?>images/btn-cancel.gif" alt="Cancel" title ="Cancel" /></a>
        	<input type="image" src="<?php echo WP_FIELDS_URL ?>images/btn-submit.gif" id="btn-submit" />
     </div>
     </form>
     </div>
    <div class="field-items">
    	<h3 class="title">Fields</h3>
        <div class="items radio">
        	Radio button <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            
            <div class="options">
                <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
                <a href="" class="addmore">Add more</a>
            </div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <!--<input type="hidden" class="hidden_deleted" value="0" name="field[{id}][deleted]">-->
            <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items check">
        	Check boxes <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            
            <div class="options">
                <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
                <a href="" class="addmore">Add more</a>
            </div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <!--<input type="hidden" class="hidden_deleted" value="0" name="field[{id}][deleted]">-->
            <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items text">
        	Text box <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            
            <div class="options">
                <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
            </div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <!--<input type="hidden" class="hidden_deleted" value="0" name="field[{id}][deleted]">-->
            <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items select">
        	Dropdown / Select <span class="field-name"></span>
            <a href="" class="toggle"></a> 
            <a href="" class="del"></a>
           
            <div class="options">
                <label>Name: <input type="text" class="txt-name" name="" /></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
                <a href="" class="addmore">Add options</a>
            </div>
           <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
           <!--<input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
           <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items textarea">
        	Textarea <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
           
            <div class="options">
            	<label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
                <label><input type="checkbox" class="chkbox" name="type_textarea_options" /> Use HTML editor: </label>
            </div>
           <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
          <!-- <input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
           <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items filefield">
        	Field field <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
           
             <div class="options">
               <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
            </div>
           <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
           <!--<input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
           <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items date-pickr">
        	Date pickr <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            
            <div class="options">
               <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
            </div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <!--<input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
            <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="items color-pickr">
        	Color pickr <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            
            <div class="options">
               <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
            </div>
           <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
           <!--<input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
           <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
        <div class="hr">&nbsp;</div>
         <div class="items post">
        	Post / Page <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
           
            <div class="options">
               <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
            </div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <!--<input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
            <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
         <div class="items taxonomy">
        	Taxonomy <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            <div class="options">
                <label>Name: <input type="text" class="txt-name" name=""/></label>
                <label>Desc: <input type="text" class="txt-desc" name="" /></label>
            </div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <!--<input type="hidden" class="hidden_deleted" value="" name="field[{id}][deleted]">-->
            <input type="hidden" class="wp_field_group_type" value="" name="">
        </div>
    </div>
            
		<?php
		}
		
		
		if ("edit-field-group-save" == $action) {
			if ($_POST) {
				//var_dump($_POST);
				$field_group_id = (int) $_POST["field_group_id"];
				
				$field_groups[$field_group_id]["name"] = stripslashes($_POST["field_group_name"]);
				$field_groups[$field_group_id]["description"] = stripslashes($_POST["field_group_description"]);
				$field_groups[$field_group_id]["repeatable"] = (bool) (isset($_POST["field_group_repeatable"]));
								
				$field_groups[$field_group_id]["fields"] = (array) stripslashes_deep($_POST["field"]);
					
				
				if (sizeof($field_groups[$field_group_id]["fields"]) == 1 && empty($field_groups[$field_group_id]["fields"][0])) {
					unset($field_groups[$field_group_id]["fields"][0]);
				}
				
				$field_groups[$field_group_id]["type_textarea_options"] = (array) @$_POST["type_textarea_options"];
				/*$field_groups[$field_group_id]["type_radiobuttons_options"] = (array) @$_POST["type_radiobuttons_options"];
				$field_groups[$field_group_id]["type_taxonomy_options"] = (array) @$_POST["type_taxonomy_options"];*/
				
				update_option("wp_fields_groups", $field_groups);
				
				$wp_fields_did_save = true;
			}
			
			$action = "";
		}
		
		
		//Delete field group
		if ("delete-field-group" == $action) {
			$field_group_id = (int) $_GET["group-id"];
			$field_groups[$field_group_id]["deleted"] = true;
			update_option("wp_fields_groups", $field_groups);
			$wp_fields_did_delete = true;
			$action = "";
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		// overview, if no action
		if (!$action) {
 
			/**
			 * view existing field groups
			 */	
			?>
 			<a href="<?php echo  WP_FIELDS_FILE."&amp;action=edit-field-group&amp;group-id=0" ?>" class="create-vew-group">Create new group</a>
            
			<div class="hr hr-listing">&nbsp;</div>
            <?php
				if (isset($wp_fields_did_save) && $wp_fields_did_save) {
					?><div id="message" class="updated"><p>Field group saved</p></div><?php
				} elseif (isset($wp_fields_did_delete) && $wp_fields_did_delete) {
					?><div id="message" class="updated"><p>Field group deleted</p></div><?php
				}
			?>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <th width="5%" scope="col">Slno</th>
                <th width="35%" scope="col">Name</th>
                <th width="50%" scope="col">Description</th>
                <th width="10%" scope="col"></th>
              </tr>
              
              
             <?php				
				$field_group_count = 0;
				foreach ($field_groups as $oneFieldGroup) {
					if (!$oneFieldGroup["deleted"]) {
						$field_group_count++;
					}
				}

				if ($field_groups == $field_group_count) {
					echo "<p>".__('No field groups yet.', 'simple-fields')."</p>";
				} else {
					
					$count = 0;
					foreach ($field_groups as $oneFieldGroup) {
						if (!$oneFieldGroup["deleted"]) {
						echo "<tr>";
							echo "<td>".++$count."</td>";
							echo "<td><a href='" . WP_FIELDS_FILE . "&amp;action=edit-field-group&amp;group-id=$oneFieldGroup[id]'>$oneFieldGroup[name]</a></td>";
							echo "<td>" . @$oneFieldGroup['description'] . "</td>";
							echo "<td><a href='" . WP_FIELDS_FILE . "&amp;action=delete-field-group&amp;group-id=$oneFieldGroup[id]'>Delete</a></td>";
						echo "</tr>";	
						}
					}
					
				}
				/*echo "<p><a class='button' href='" . EASY_FIELDS_FILE . "&amp;action=edit-field-group&amp;group-id=0'>+ ".__('New field group', 'simple-fields')."</a></p>";*/
				?>
		
        
        <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              </table>
              
              
              
				
	<?
		}
?>
 	</div>
    
<?php	
}

function wp_fields_field_group_add_field_template($fieldID, $field_group_in_edit = null) {
	
	echo "<pre>";
		//var_dump($field_group_in_edit["fields"][$fieldID]);
	echo "</pre>";
	$label = "";
	if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'text'){
		$label = "Text box";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'radio'){
		$label = "Radio button ";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'check'){
		$label = "Check boxes";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'select'){
		$label = "Dropdown / Select";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'textarea'){
		$label = "Textarea";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'filefield'){
		$label = "Field field";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'date-pickr'){
		$label = "Date pickr";	
	}else if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'color-pickr'){
		$label = "Color pickr";	
	}
	
	$out =  '<div class="items ' . $field_group_in_edit["fields"][$fieldID]["type"]  . ' ">
        	'. $label .' <span class="field-name"></span>
            <a href="" class="toggle"></a>
            <a href="" class="del"></a>
            <div class="options">
                <label>Name: <input type="text" class="txt-name" value="'.$field_group_in_edit["fields"][$fieldID]["name"].'" name="field['.$fieldID.'][name]"/></label>
                <label>Desc: <input type="text" class="txt-desc" value="'.$field_group_in_edit["fields"][$fieldID]["description"].'" name="field['.$fieldID.'][description]"/></label>';
			
			 if(trim($field_group_in_edit["fields"][$fieldID]["type"]) == 'radio'){
				 	$count = 0;
				 	$out .= '<a class="addmore" href="">Add more</a>';
					foreach($field_group_in_edit["fields"][$fieldID]["type_radiobuttons_options"] as $filedOne){
						
						var_dump($filedOne);
						//var_dump($field_group_in_edit["fields"][$fieldID]["type_radiobuttons_options"]["checked_by_default_num"]);
						$out .='<div class="more-fields">';
 						$out .= '<input type="text" value="'.$filedOne["value"].'" class="txt" name="field['.$fieldID.'][type_radiobuttons_options][radiobutton_num_'.++$count.'][value]"><input name="field['.$fieldID.'][type_radiobuttons_options][checked_by_default_num]" type="radio" value="" class="radio-default-check" /><a href="" class="del-field">Delete field</a>';
						$out .='</div>';
					};
			 }
			
			
        $out .=    '</div>
            <input type="hidden" value="" class="wp-fields-field-group-one-field-id" name="">
            <input type="hidden" class="hidden_deleted" value="0" name="field[{id}][deleted]">
        </div>';
		
	return $out;
}
/*function d($var){
	echo "<pre id='debug'>";
						var_dump($var);
					echo"<a href='javascript:void(0)' id='debugclose'>X</a></pre>";
					
} */

?>



